# 保存程序图片到七牛云

from qiniu import Auth, put_file, etag, put_data
import qiniu.config

# 需要填写你的 Access Key 和 Secret Key
access_key = 'Ia0lJfxQCY4BoqlU3t2vyYs7nBhxiIpMK6KD-Q0I'
secret_key = 'FN-f89S864c-UNf_X0lJ0g0EcU2AIbqAktkFj-e-'


def image_storage(image_data):
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'info15'
    # 上传到七牛后保存的文件名
    key = None
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    # 要上传文件的本地路径
    # localfile = './66.png'
    # ret, info = put_file(token, key, localfile)
    # 二进制传输
    ret, info = put_data(token, key, image_data)
    # print(info)
    # print(ret)
    # 判断是否上传成功
    if info.status_code == 200:
        # 获取图片名
        return ret.get('key')
    else:
        return ''

if __name__ == '__main__':
    with open('./66.png', 'rb') as f:
        image_name = image_storage(f.read())
        print(image_name)
    # f = open("./66.png", 'rb')
    # image_storage(f.read())
    # f.close()

